package view.GUI.adminView.inventory;

import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import view.GUI.mainView.KindVBox;
import view.GUI.mainView.KindVBox.BoxType;
import view.GUI.mainView.MainView;
import model.Configuration;
import model.data.tableModel.inventory.Inventory;
import model.data.wrapper.InputWrapper;
import enums.GUIText;
import enums.Status;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Modality;
import javafx.stage.Stage;


public final class InventoryView extends Application{

	private final static Logger log = LogManager.getLogger("ERROR");
	
	private Label lblDate;
	private Label lblComment;
	
	private TextField txtComment;
	
	private Button btnSave;
	private Button btnCancel;
	
	private KindVBox deposit;
	private KindVBox drinks;
	private KindVBox grocery;
	private KindVBox other;
	
	private GridPane root;
	private ScrollPane sp;
	
	private Stage primaryStage;
	private Scene scene;
	
	private MainView owner;
	
	private int rowCounter;
	private DatePicker datePicker;
	
	private Label lblTitle;
	private Separator sepTop;
	private Separator sepBottom;
	
	public InventoryView(MainView mainView){
		this.owner = mainView;
		this.rowCounter = 1;
		createWidgets();
		addWidgets();
		setupLayout();
		setupInteraction();
		try {
			start(new Stage());
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}	

	private void createWidgets() {
		lblTitle = new Label(GUIText.INVENTORY.toString());
		sepTop = new Separator();
		sepBottom = new Separator();
		lblDate = new Label(GUIText.DATE.toString());
		lblComment = new Label(GUIText.COMMENT.toString());
		
		datePicker = new DatePicker();
		txtComment = new TextField();
		
		btnSave = new Button(GUIText.SAVE.toString());
		btnCancel = new Button(GUIText.CANCEL.toString());
		
		deposit = new KindVBox(BoxType.DEPOSIT, owner, owner.getProducts(false, BoxType.DEPOSIT));
		drinks = new KindVBox(BoxType.DRINKS, owner, owner.getProducts(false, BoxType.DRINKS));
		grocery = new KindVBox(BoxType.GROCERIES, owner, owner.getProducts(false, BoxType.GROCERIES));
		other = new KindVBox(BoxType.OTHER, owner, owner.getProducts(false, BoxType.OTHER));
		
		root = new GridPane();
		sp = new ScrollPane();
	}


	private void addWidgets() {
		root.add(lblTitle, 0, rowCounter++);
		root.add(sepTop, 0, rowCounter++);
		
		root.add(lblDate, 0, rowCounter);
		root.add(datePicker, 1, rowCounter++);
		
		root.add(lblComment, 0, rowCounter);
		root.add(txtComment, 1, rowCounter++);

		root.add(deposit, 0, rowCounter++);

		root.add(drinks, 0, rowCounter++);
		
		root.add(grocery, 0, rowCounter++);
		root.add(other, 0, rowCounter++);
		
		root.add(btnSave, 0, rowCounter);
		root.add(btnCancel, 1, rowCounter++);
		root.add(sepBottom, 0, rowCounter++);
		
		sp.setContent(root);
	}
	
	public Inventory getInventory(){
		Inventory inv = Inventory.createNewInventory(datePicker.getValue().toString().substring(0, 7), txtComment.getText(), Status.ACTIVE, null);
		return inv;
	}


	private void setupLayout() {
		GridPane.setColumnSpan(other, 3);
		GridPane.setColumnSpan(grocery, 3);		
		GridPane.setColumnSpan(drinks, 3);
		GridPane.setColumnSpan(deposit, 3);
		GridPane.setColumnSpan(sepTop, 3);
		GridPane.setColumnSpan(sepBottom, 3);
		root.getStyleClass().add("grid");
		lblTitle.setId("topic");
	}


	private void setupInteraction() {
		btnCancel.setOnAction(new EventHandler<ActionEvent>(){

			@Override
			public void handle(ActionEvent event) {
				primaryStage.close();
			}
		});
		
		btnSave.setOnAction(new EventHandler<ActionEvent>(){

			@Override
			public void handle(ActionEvent event) {
				List<List<InputWrapper>> inputList = new ArrayList<>();
				inputList.add(deposit.getInput());
				inputList.add(drinks.getInput());
				inputList.add(grocery.getInput());
				inputList.add(other.getInput());
				
				owner.createInventoryInvoice(inputList, getInventory());
				primaryStage.close();
			}
		});
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		this.scene = new Scene(sp);
		scene.getStylesheets().add(getClass().getResource(Configuration.STYLEMAINVIEW).toExternalForm());
		scene.getStylesheets().add(getClass().getResource(Configuration.STYLEROOT).toExternalForm());
		this.primaryStage = primaryStage;
		this.primaryStage.initModality(Modality.WINDOW_MODAL);
		primaryStage.initOwner(owner.getPrimaryStage());
		primaryStage.setScene(scene);
		primaryStage.setTitle(GUIText.INVENTORY.toString());
		primaryStage.setResizable(true);
		primaryStage.setWidth(650);
		primaryStage.setHeight(700);
		primaryStage.centerOnScreen();
		primaryStage.show();
	}
}
